package kyle.com;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.LinearLayout;

/**
 * Created by kyle on 2017/3/8.
 * 滑动显示删除按钮
 */
public class ScrollLinearLayout extends LinearLayout {
    private GestureDetectorCompat gestureDetectorCompat;
    private int DELETE_WIDTH;//删除按钮的宽度

    public ScrollLinearLayout(Context context) {
        super(context, null, 0);
    }

    public ScrollLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ScrollLinearLayout, 0, 0);
        DELETE_WIDTH = (int) array.getDimension(0, 90);//我只设置了一个属性，就是删除按钮的宽度，所以取0位置的属性，默认值为90px
        gestureDetectorCompat = new GestureDetectorCompat(context, new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                return true;//在这里返回TRUE，滑动的时候，子控件的点击事件就不会响应，返回false代表滑动的时候，子控件也可以响应点击事件
            }

            @Override
            public void onLongPress(MotionEvent e) {
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                return false;
            }
        });
    }

    private float x = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = event.getX();//手指按下，记录初次x
                break;
            case MotionEvent.ACTION_MOVE:
                int shouldMoveX;
                if (getScrollX() + x - event.getX() > DELETE_WIDTH) {//如果当前ScrollX加上即将要滑动的数值大于我们设置的值，则手动设置剩余应该滑动的值
                    shouldMoveX = DELETE_WIDTH - getScrollX();
                } else if (getScrollX() + x - event.getX() <= 0) {//如果将要滑动的x为负，则置为0,并且（不然会一直往右滑的）
                    shouldMoveX = 0;
                } else {
                    shouldMoveX = (int) (x - event.getX());
                }
                scrollBy(shouldMoveX, 0);
                x = event.getX();//重设x
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL://手指抬起或事件取消，都会调用这段矫正代码，
                if (getScrollX() > 0 && getScrollX() <= DELETE_WIDTH / 2) {
                    scrollTo(0, 0);
                } else if (getScrollX() > DELETE_WIDTH / 2 && getScrollX() <= DELETE_WIDTH) {
                    scrollTo(DELETE_WIDTH, 0);
                }
                break;
        }
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return gestureDetectorCompat.onTouchEvent(ev);
    }
}
